<?php

namespace app\controller;


use app\service\LimitedTimeShopService;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Db;

//钻石商店控制器
class DiamondShopController extends Base
{

    /**
     * 获取钻石商店信息
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     */
    public function list(): Response
    {
        $res = (new LimitedTimeShopService())->list($this->user_id);
        return $this->json($res);
    }


    //购买商品
    public function buy(): Response
    {
        //购买商品
        $limited_time_shop_id = $this->request->post('limited_time_shop_id');
        if (empty($limited_time_shop_id)) {
            return $this->fail('参数错误');
        }
        // 启动事务
        Db::startTrans();

        try {
            $res = (new LimitedTimeShopService())->buy($this->user_id, $limited_time_shop_id);

            if ($res['code']) {
                Db::commit();
            } else {
                Db::rollback();
            }
            return $this->json($res);

        } catch (\Exception $e) {
            Db::rollBack();
            return $this->fail("服务器异常".$e->getLine());
        }

    }


}